module(..., package.seeall)
local director 	 = require("director")
local localGroup = display.newGroup()
local gui        = require "scr.mainGame.guiConfig"
local movieClip  = require "libs.movieclip"
local funCommon  = require "libs.libNetwork"	
function new()
	local startTime  = nil
	local scollGroup = display.newGroup()
	local selected   = nill      -- id duoc chon	
	local pick       = {0, 0, 0, 0, 0, 0 }		  -- mang chua cac o dat cuoc va gia tien
	local deltaX     = 0     -- don vi tang khi cuon
	local user       = {}       -- thong tin user dang cuoc
	local btCircle   = {}		  -- button vong trong
	local minScollX  = gui.showScoll.x
	local maxScollX  = gui.scoll.x + gui.scoll.scale * gui.scoll.w / 2 - 15
	local maxMoney   = 10000
	local oldDelta   = deltaPick
	local itemSelected = {}
	local localGroup   = display.newGroup()
	local sizeScoll    = maxScollX - minScollX
	
	local bg = display.newImage(gui.bGround.fn )
	bg.x     = gui.bGround.x
	bg.y     = gui.bGround.y
	bg:scale(gui.bGround.scale, gui.bGround.scale)
	
	local border = display.newImage(gui.border.fn )
	border.x     = gui.border.x
	border.y     = gui.border.y
	border:scale(gui.border.scale, gui.border.scale)
	
	local tbPlay = display.newImage( gui.tbPlay.fn )
	tbPlay.x     = gui.tbPlay.x
	tbPlay.y     = gui.tbPlay.y
	tbPlay:scale(gui.tbPlay.scale, gui.tbPlay.scale)
	
	local uAvarta = display.newImage( gui.uAvarta.fn )
	uAvarta.x     = gui.uAvarta.x
	uAvarta.y     = gui.uAvarta.y
	uAvarta:scale(gui.uAvarta.scale, gui.uAvarta.scale)
	
	local text = display.newImage( gui.text.fn )
	text.x     = gui.text.x
	text.y     = gui.text.y
	text:scale(gui.text.scale, gui.text.scale) 
	
	local scoll = display.newImage( gui.scoll.fn )
	scoll.x     = gui.scoll.x
	scoll.y     = gui.scoll.y
	scoll:scale(gui.scoll.scale, gui.scoll.scale)
	
	local showScoll = display.newImage( gui.showScoll.fn )
	showScoll.x     = gui.showScoll.x
	showScoll.y     = gui.showScoll.y
	showScoll:scale(gui.showScoll.scale, gui.showScoll.scale)
	
	local btOk = display.newImage( gui.btOk.fn )
	btOk.x     = gui.btOk.x
	btOk.y     = gui.btOk.y
	btOk:scale(gui.btOk.scale, gui.btOk.scale)
	
	local dishes = display.newImage( gui.dishes.fn )
	dishes.x     = gui.dishes.x
	dishes.y     = gui.dishes.y
	dishes:scale( gui.dishes.scale, gui.dishes.scale )
	
	for i = 1, 6 do 
		itemSelected[i] = display.newImage( gui.icChoose[i].fn )
		itemSelected[i].x  = gui.icChoose[i].x
	    itemSelected[i].y  = gui.icChoose[i].y
		itemSelected[i]:scale(gui.icChoose[i].scale, gui.icChoose[i].scale)
		itemSelected[i].isVisible = false
		itemSelected[i].money = display.newImage( gui.moneyIcon.fn )
		itemSelected[i].money.x = gui.btCirclePosition[i].x+20
		itemSelected[i].money.y = gui.btCirclePosition[i].y
		itemSelected[i].money:scale( gui.moneyIcon.scale, gui.moneyIcon.scale )
		itemSelected[i].money.isVisible = false
	end
	
	--Vo hieu hoa cac EventListener
	function clearListener()
		return true
	end
	
	local imgClear = display.newImage( gui.imgClear.fn )
	imgClear.x     = gui.imgClear.x
	imgClear.y     = gui.imgClear.y
	imgClear:scale(gui.imgClear.scale, gui.imgClear.scale) 
	imgClear:addEventListener("touch", clearListener)
	imgClear:addEventListener("tap", clearListener)
	imgClear.isVisible = false
	
	local transparentRect  = display.newRect( 0, 0, 480, 320 )
	transparentRect.alpha  = 0
	
	money = display.newText(" ", gui.showScoll.x, 239, native.systemFont, 11 )
	money:setTextColor( 0, 0, 0 )
	
	-- Bat dau choi
	function start()
		startTime = event.time
	end
	
	-- chon o dat cuoc
	function getPickSelect(event)
		showScoll.x = gui.showScoll.x
		if selected ~= nil then
			itemSelected[selected].isVisible = false
			maxMoney = maxMoney - pick[selected]
		end
		selected = event.target.id
		maxMoney = maxMoney + pick[selected]
		itemSelected[selected ].isVisible = true
		if maxMoney == 0 then 
			showScoll.x = maxScollX
		else
			deltaX = pick[selected] * sizeScoll / maxMoney
			showScoll.x = showScoll.x + deltaX
		end
		money.x     = showScoll.x
		money.text  =  pick[selected]
		scollGroup.isVisible = true
	end
	
	for i = 1, 6 do 
		local a = display.newCircle(gui.btCirclePosition[i].x, gui.btCirclePosition[i].y ,30)
		a:addEventListener("tap" ,getPickSelect)
		localGroup:insert(a)
		a.id = i
	end
	
	-- lam mo diplay
	function changeTransparent( value )
		transparentRect.alpha = value
	end
	
	-- thiet lap
	function getBtSetting()
		changeTransparent(0.2)
		saveSetting("off", "off")
	end
	
	-- chia se
	function getBtshare()
	end
	
	-- chat
	function getBtChat()
	end
	
	-- cap nhat lai so tien cuoc
	function updateMoney()
		
	end
	
	--khi cuon
	function getScoll(event)
		local phase = event.phase
		local t = event.target
		if "began" == phase then
			display.getCurrentStage():setFocus( t )
			t.isFocus = true
			oldX    = t.x
			t.x0 	= event.x - t.x
		elseif t.isFocus then
			if "moved" == phase then
				t.x    = event.x - t.x0
				deltaX = t.x - minScollX
				if t.x  < minScollX then
					t.x = minScollX
					deltaX = 0
				end
				if t.x > maxScollX then
					t.x    = maxScollX
					deltaX = sizeScoll
				end
				money.x     = showScoll.x
				money.text  = math.floor( (maxMoney * deltaX )/ sizeScoll)
			elseif "ended" == phase or "cancelled" == phase then
				display.getCurrentStage():setFocus( nil )
				t.isFocus = false
			end
		end
		return true	
	end
	
	
	
    --khi nhan back
	function pressBack()
	end
	
	-- Gieo xuc xac
	function sowingDice()
		dishes.isVisible   = false
		imgClear.isVisible = true
		local dice = movieClip.newAnim  {
											gui.diceClip.img[1],
											gui.diceClip.img[2],
											gui.diceClip.img[3],
											gui.diceClip.img[4],
											gui.diceClip.img[5],
											gui.diceClip.img[6]
									    } 
						
		dice.x    = gui.diceClip.x
		dice.y	  = gui.diceClip.y
		dice:play()
		--audioDice = audio.loadSound( gui.diceClip.sound )
		--audio.play( audioDice,{channel = 1,loops = -1})
		processSound( gui.diceClip.sound, 1, -1, true )
		stopClip =  function ()
						dice:stopAtFrame(4)
						processSound( gui.diceClip.sound, 1, -1, false )
						dice.isVisible     = false
						dishes.isVisible   = true
						imgClear.isVisible = false
					end
		timeLacXS = timer.performWithDelay( 4000, stopClip )
	end
	
	--khi nhan ok 
	function pressOk(event)
		-- gui len sever xu ly
		
		
		-- sau khi sever tra ve
		
		if selected ~= nill then
			pick[selected] = math.floor( (maxMoney * deltaX) / sizeScoll)
			--print(pick[selected])
			if pick[selected] ~= 0 then
				itemSelected[selected].money.isVisible = true
			else
				itemSelected[selected].money.isVisible = false
			end
			
			maxMoney = maxMoney - pick[selected]
		end
		showScoll.x = gui.showScoll.x
		li
		scollGroup.isVisible = false
		itemSelected[selected].isVisible = false
		--sowingDice()
		selected = nil
	end
	
	showScoll:addEventListener("touch", getScoll)
	btOk:addEventListener("tap", pressOk)
	
	localGroup:insert(bg)
	localGroup:insert(border)
	localGroup:insert(tbPlay)
	localGroup:insert(uAvarta)
	localGroup:insert(text)
	scollGroup:insert(scoll)
	scollGroup:insert(btOk)
	scollGroup:insert(showScoll)
	localGroup:insert(dishes)
	scollGroup:insert(money)
	scollGroup.isVisible = false
	localGroup:insert(scollGroup)
	localGroup:insert(imgClear)
	localGroup:insert(transparentRect)
    for i = 1, 6 do 
		localGroup:insert(itemSelected[i])
		localGroup:insert(itemSelected[i].money)
	end
	
	-- hien thi ket qua gieo xuc xac
	function resultSowing( result, status )
	
	end
		
	function stopAll()
		
		scollGroup.isVisible = false
	end
	
	function checkTimeOut(event)
		if startTime ~= nil then
			if event.time - startTime > 30000 then
				stopAll()
			end
		end
	end
	return localGroup
end